import { roleAdd, roleLook } from "../../api/system";
import { userLogin } from "../../api/user";
export const LOGIN_CENTRE = "LOGIN_CENTRE";
export const LOGIN_SUCCESS = "LOGIN_SUCCESS";
export const LOGIN_FAIL = "LOGIN_FAIL";

// 加载
export const login_centre = () => {
  return {
    type: LOGIN_CENTRE,
  };
};

// 成功
export const login_success = (userInfo) => {
  return {
    type: LOGIN_SUCCESS,
    userInfo,
  };
};
// 失败
export const login_fail = () => {
  return {
    type: LOGIN_FAIL,
  };
};

// 异步登录三步走
export const login_asyn = (userInfo, history) => {
  return (dispatch) => {
    dispatch(login_centre());
    userLogin(userInfo)
      .then(async (res) => {
        const roleInfo = await roleLook(res.data.roleId);
        console.log(roleInfo);
        dispatch(login_success({ ...res.data, power: roleInfo.data.power }));
        history.push("/admin/dashboard");
      })
      .catch((err) => {
        dispatch(login_fail());
      });
  };
};
